package com.test.hongbao;

import android.app.Notification;
import android.app.PendingIntent;
import android.os.Build;
import android.service.notification.NotificationListenerService;
import android.service.notification.StatusBarNotification;

/**
 * Created by jiangyu on 2016/2/5.
 */
public class HongbaoNotificationListenerService extends NotificationListenerService {
    @Override
    public void onNotificationPosted(StatusBarNotification sbn) {
        if (HongbaoPreference.getInstance(this.getApplicationContext()).isAutoHongbao()) {
            Notification mNotification = sbn.getNotification();
            if (mNotification != null) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                    if (!mNotification.extras.getString("android.text").contains("[微信红包]恭喜发财")) {
                        return;
                    } else {
                        PendingIntent pendingIntent = mNotification.contentIntent;
                        try {
                            pendingIntent.send();
                        } catch (PendingIntent.CanceledException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    }

    @Override
    public void onNotificationRemoved(StatusBarNotification sbn) {

    }
}
